/*
 * @Author: Logs404
 * @LastEditors: Logs404
 * 可以输入预定的版权声明、个性签名、空行等
 */
var http = require('http');
var url = require('url');
var util = require('util');
var querystring = require('querystring');

var html = `
<head>
    <title>喵喵喵</title>
</head>
<body>
    <h1>测试</h1>
    <form action="/" method="post">
    <input type="text" name="name">
    <input type="text" name="password">
    <button type="submit">确认</button>
    </form>
</body>
`

function init(router) {
    function event(request, response) {
        let post = ''
        request.on('data', function (data) {
            post += data;
        })

        request.on('end', function () {
            post = querystring.parse(post)
            response.writeHead(200, { 'Content-Type': 'text/html;charset=UTF-8' })
            // let params = url.parse(request.url,true).query
            // router(params)
            if (post.name && post.password){
                response.write(`
                用户名: ${post.name}
                <br/>
                密码: ${post.password}
                `)
            }else{
                response.write(html)
            }
                response.end()
        })
    }
    http.createServer(event).listen(2333)
}

exports.init = init